package com.hsnn.medstgmini.pro.base.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.base.dict.model.form.DicAreaForm;
import com.hsnn.medstgmini.base.dict.service.DicAreaManager;
import com.hsnn.medstgmini.base.dict.service.DicSysManager;
import com.hsnn.medstgmini.base.std.model.HospInfo;
import com.hsnn.medstgmini.base.std.model.StdCompinfo;
import com.hsnn.medstgmini.pro.base.model.form.HospForm;
import com.hsnn.medstgmini.pro.base.service.BaseHospManager;
import com.hsnn.medstgmini.pro.project.service.BidProjectManager;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.util.AreaUtil;
import com.hsnn.medstgmini.util.Pagination;


@Controller
@RequestMapping("/proBaseHosp")
public class BaseHostController {


	@Autowired
	private BaseHospManager baseHospManager;
	@Autowired
	private BidProjectManager bidProjectManager;
	@Autowired
	private DicSysManager dicSysManager;
	
	@Autowired
	private DicAreaManager dicAreaManager;
	
	@Autowired
	private  HttpServletRequest request;
	
    @Value("#{sysProp['system.province']}")
    private String sysProvince;
    
    @Autowired
	private HttpSession session;
	
	private SysUser getUser() {
		return (SysUser) session.getAttribute(Constants.USERINFO);
	}
    
	/**
	 * @category 进入企业商品列表页面
	 * @auth 陈镜宇
	 * @param model
	 * @param bidProject
	 * @return
	 * @date 2015年6月15日
	 */
	@RequestMapping("/toHospList")
	public String toBaseGoodsListByComp(Model model) {
		SysUser user=(SysUser) request.getSession().getAttribute(Constants.USERINFO); 
		
		String hospId=null;
		if (user.getType() == 2) {//生产企业
			hospId = ((StdCompinfo)user.getInfo()).getCompId();
		} else if (user.getType() == 3) {//配送企业
			hospId =((StdCompinfo)user.getInfo()).getCompId();
		}
		
		model.addAttribute("projList", bidProjectManager.getBidProjectListByCompId(hospId, user.getType().toString()));
		model.addAttribute("clList", dicSysManager.getDicSysByClassifyCode(Constants.HOSP_CAT));
		
		DicAreaForm area = dicAreaManager.getDicAreaById(sysProvince);
		request.setAttribute("area", area);
		request.setAttribute("sysAreaId", sysProvince);
		
		return "/pro/base/baseHospList";
	}
	/**
	 * 
	 * @category 获取医疗机构列表目录列表Json
	 * @auth 单大伟
	 * @param projId
	 * @return
	 * @date 2015年6月11日
	 */
	@ResponseBody
	@RequestMapping("/getBaseHospList")
	public Pagination getBaseHospList(String projId, String hospName, String hospCl, String hospCode, String area1Id, String area2Id){
		Pagination page = new Pagination(request);
		SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO);
		String compId = null;
		if (user.getType() == 2 || user.getType() == 3) {//生产企业，配送企业
			compId = ((StdCompinfo)user.getInfo()).getCompId();
		} else if (user.getType() == 4) {//医疗机构
			compId = ((HospInfo)user.getInfo()).getHospId();
		}
		page.getConditions().put("compId", compId);
		page.getConditions().put("compType", user.getType().toString());
		if(StringUtils.isNoneBlank(projId)){
			page.getConditions().put("projId", projId);
		}
		if(StringUtils.isNoneBlank(hospName)){
			page.getConditions().put("hospName", hospName);
		}
		if(StringUtils.isNoneBlank(hospCl)){
			page.getConditions().put("hospCl", hospCl);
		}
		if(StringUtils.isNoneBlank(hospCode)){
			page.getConditions().put("hospCode", hospCode);
		}
		
		String areaId = AreaUtil.getAreaStartCode(sysProvince, area1Id, area2Id);
		page.getConditions().put("areaId", areaId);
		return baseHospManager.getHostListByPage(page);
		
	}
	
	/**
	 * @category 中心查看医疗机构目录
	 * @date 2015年11月12日
	 * @return
	 */
	@RequestMapping("/toHospListByZX")
	public String toHospListByZX(Model model) {
		DicAreaForm area = dicAreaManager.getDicAreaById(sysProvince);
		model.addAttribute("clList", dicSysManager.getDicSysByClassifyCode(Constants.HOSP_CAT));
		model.addAttribute("area", area);
		model.addAttribute("sysAreaId", sysProvince);
		return "/pro/base/catalog/hospListByZX";
	}
	
	/**
	 * @category 中心查看医疗机构目录
	 * @date 2015年11月12日
	 * @param hospForm
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getHospListByZX")
	public Pagination getHospListByZX(HospForm hospForm) {
		Pagination page = new Pagination(request);
		String classifyCode = request.getParameter("classifyCode");
		page.getConditions().put("classifyCode", classifyCode);
		String hospType = request.getParameter("hospType");
		page.getConditions().put("hospType", hospType);
		page.setForm(hospForm);
		baseHospManager.getHospListByZX(page);
		return page;
	}
	
	/**
	 * @category 卫生局查看医疗机构目录
	 * @date 2015年11月12日
	 * @return
	 */
	@RequestMapping("/toHospListByWSJ")
	public String toHospListByWSJ(Model model) {
		DicAreaForm area = dicAreaManager.getDicAreaById(sysProvince);
		model.addAttribute("clList", dicSysManager.getDicSysByClassifyCode(Constants.HOSP_CAT));
		model.addAttribute("area", area);
		model.addAttribute("sysAreaId", sysProvince);
		return "/pro/base/catalog/hospListByWSJ";
	}
	
	/**
	 * @category 卫生局查看医疗机构目录
	 * @date 2015年11月12日
	 * @param hospForm
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getHospListByWSJ")
	public Pagination getHospListByWSJ(HospForm hospForm) {
		Pagination page = new Pagination(request);
		String hospClassifyIds = request.getParameter("hospClassifyIds");
		page.getConditions().put("hospClassifyIds", hospClassifyIds);
		String hospType = request.getParameter("hospType");
		page.getConditions().put("hospType", hospType);
		page.setForm(hospForm);
		baseHospManager.getHospListByWSJ(page, getUser());
		return page;
	}
	
	/**
	 * @category 医疗机构查看医疗机构目录
	 * @date 2015年11月12日
	 * @return
	 */
	@RequestMapping("/toHospListByYY")
	public String toHospListByYY(Model model) {
		DicAreaForm area = dicAreaManager.getDicAreaById(sysProvince);
		model.addAttribute("clList", dicSysManager.getDicSysByClassifyCode(Constants.HOSP_CAT));
		model.addAttribute("area", area);
		model.addAttribute("sysAreaId", sysProvince);
		return "/pro/base/catalog/hospListByYY";
	}
	
	/**
	 * @category 医疗机构查看医疗机构目录
	 * @date 2015年11月12日
	 * @param hospForm
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getHospListByYY")
	public Pagination getHospListByYY(HospForm hospForm) {
		Pagination page = new Pagination(request);
		SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO); 
		HospInfo hospInfo = user.getInfo();
		hospForm.setHospCode(hospInfo.getHospCode());
		page.setForm(hospForm);
		baseHospManager.getHospListByYY(page);
		return page;
	}
	
	/**
	 * @category 生产企业查看医疗机构目录
	 * @date 2015年11月12日
	 * @return
	 */
	@RequestMapping("/toHospListBySC")
	public String toHospListBySC(Model model) {
		DicAreaForm area = dicAreaManager.getDicAreaById(sysProvince);
		model.addAttribute("clList", dicSysManager.getDicSysByClassifyCode(Constants.HOSP_CAT));
		model.addAttribute("area", area);
		model.addAttribute("sysAreaId", sysProvince);
		return "/pro/base/catalog/hospListBySC";
	}
	
	/**
	 * @category 生产企业查看医疗机构目录
	 * @date 2015年11月12日
	 * @param hospForm
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getHospListBySC")
	public Pagination getHospListBySC(HospForm hospForm) {
		Pagination page = new Pagination(request);
		SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO); 
		StdCompinfo compInfo = user.getInfo();
		hospForm.setCompCode(compInfo.getCompCode());
		page.setForm(hospForm);
		baseHospManager.getHospListBySC(page);
		return page;
	}
	
	/**
	 * @category 配送企业查看医疗机构目录
	 * @date 2015年11月12日
	 * @return
	 */
	@RequestMapping("/toHospListByPS")
	public String toHospListByPS(Model model) {
		DicAreaForm area = dicAreaManager.getDicAreaById(sysProvince);
		model.addAttribute("clList", dicSysManager.getDicSysByClassifyCode(Constants.HOSP_CAT));
		model.addAttribute("area", area);
		model.addAttribute("sysAreaId", sysProvince);
		return "/pro/base/catalog/hospListByPS";
	}
	
	/**
	 * @category 配送企业查看医疗机构目录
	 * @date 2015年11月12日
	 * @param hospForm
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getHospListByPS")
	public Pagination getHospListByPS(HospForm hospForm) {
		Pagination page = new Pagination(request);
		SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO); 
		StdCompinfo compInfo = user.getInfo();
		hospForm.setCompCode(compInfo.getCompCode());
		page.setForm(hospForm);
		baseHospManager.getHospListByPS(page);
		return page;
	}
	
}
